<?php /** @var Commodity $thatCommodity */ ?>
<?php if (isset($thatCommodity)): ?>
    <div id="edit-commodity-dialog">
        <form id="edit-commodity-form">
            <input type="hidden" name="commodity_id" value="<?php echo $thatCommodity->getId(); ?>"/>
            <input type="hidden" name="user_id" value="<?php echo $thatCommodity->getUserId(); ?>"/>

            <div class="left-right-side">
                <div class="input-group left-side">
                    <label for="commodity-title" class="input-group-addon">发布标题</label>
                    <input type="text" name="commodity_title" id="commodity-title" class="form-control" maxlength="30" required="required" placeholder="请填写商品名称" value="<?php echo $thatCommodity->getTitle() ?>"/>
                </div>
                <div class="input-group right-side">
                    <label for="release-type" class="input-group-addon">发布类型</label>
                    <select id="release-type" name="release_type" class="form-control">
                        <option value="1" <?php echo $thatCommodity->getReleaseType() == 1 ? 'selected="selected"' : '' ?>>全新</option>
                        <option value="2" <?php echo $thatCommodity->getReleaseType() == 2 ? 'selected="selected"' : '' ?>>二手</option>
                        <option value="3" <?php echo $thatCommodity->getReleaseType() == 3 ? 'selected="selected"' : '' ?>>赠送</option>
                    </select>
                </div>
            </div>
            <div class="left-right-side">
                <div class="input-group left-side">
                    <label for="commodity-category" class="input-group-addon">物品类型</label>
                    <select id="commodity-category" name="commodity_category" class="form-control" style="float:left;width: 32%;">
                        <?php if (isset($MasterCommodityCategoryArray) && sizeof($MasterCommodityCategoryArray) > 0) : ?>
                            <?php foreach ($MasterCommodityCategoryArray as $index => $thisCommodityCategory) : ?>
                                <?php /** @var CommodityCategory $thisCommodityCategory */ ?>
                                <option id="commodity-category-<?php echo $thisCommodityCategory->getId(); ?>" <?php if($thatCommodity->getCommodityCatId()==$thisCommodityCategory->getId()||$thatCommodity->getCommodityCatFaId()==$thisCommodityCategory->getId()) echo 'selected="selected"' ?>  value="<?php echo $thisCommodityCategory->getId(); ?>">
                                    <?php echo $thisCommodityCategory->getName(); ?>
                                </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>

                    <select id="commodity-sub-category" name="commodity_sub_category" class="form-control" style="float:left; width:68%;">
                        <?php if (isset($NotMasterCommodityCategoryArray) && sizeof($NotMasterCommodityCategoryArray) > 0) : ?>
                            <?php foreach ($NotMasterCommodityCategoryArray as $index => $thisCommodityCategory) : ?>
                                <?php /** @var CommodityCategory $thisCommodityCategory */ ?>
                                <option class="commodity-sub-category-option" value="<?php echo $thisCommodityCategory->getId(); ?>" data-father-id="<?php echo $thisCommodityCategory->getFatherId() ?>" <?php if($thatCommodity->getCommodityCatId()==$thisCommodityCategory->getId()) echo 'selected="selected"' ?> style="display: none">
                                    <?php echo $thisCommodityCategory->getName(); ?>
                                </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="input-group right-side">
                    <label for="commodity-price" class="input-group-addon">商品价格</label>
                    <input type="number" id="commodity-price" name="commodity_price" class="form-control" min="0" max="10000000" placeholder="请输入售价" value="<?php echo $thatCommodity->getPrice() ?>"/>
                </div>
            </div>
            <div class="left-right-side">
                <div class="input-group left-side">
                    <label for="commodity-contact-info" class="input-group-addon">联系方式</label>
                    <select id="commodity-contact-type" name="commodity_contact_type" class="form-control" style="float:left; width:32%">
                        <option value="<?php echo _ContactType_LongPhone ?>" <?php if($thatCommodity->getContactType()==_ContactType_LongPhone) echo "selected"?> >长号</option>
                        <option value="<?php echo _ContactType_ShortPhone ?>" <?php if($thatCommodity->getContactType()==_ContactType_ShortPhone) echo "selected"?>>短号</option>
                        <option value="<?php echo _ContactType_Qq ?>" <?php if($thatCommodity->getContactType()==_ContactType_Qq) echo "selected"?>>QQ</option>
                    </select>
                    <input type="text" id="commodity-contact-info" name="commodity_contact_info" class="form-control" style="float:left; width:68%" value="<?php echo $thatCommodity->getContactInfo() ?>"/>
                </div>
                <div class="input-group right-side">
                    <label for="commodity-stock" class="input-group-addon">物品库存</label>
                    <input type="number" id="commodity-stock" name="commodity_stock" class="form-control" min="0" max="10000" placeholder="请输入库存" value="<?php echo $thatCommodity->getStock() ?>"/>
                </div>
            </div>
            <div class="input-group">
                <label for="commodity-intro" class="input-group-addon">商品简介</label>
                <input type="text" id="commodity-intro" name="commodity_intro" class="form-control" required="required" placeholder="请输入商品简介" value="<?php echo $thatCommodity->getIntro() ?>"/>
            </div>
            <div class="input-group-has-textarea">
                <label for="commodity-content" class="input-group-addon">商品描述</label>
                <textarea id="commodity-content" name="commodity_content" class="" rows="4" placeholder="请输入商品描述" style="height:300px;"><?php echo $thatCommodity->getContent() ?></textarea>
            </div>
            <div class="input-group">
                <input type="submit" class="form-control" value="确认修改"/>
            </div>
        </form>
    </div>
<?php endif; ?>

<script>
    $(function () {
        $("#edit-commodity-form").validate({
            submitHandler: function (form) {
                $.ajax({
                    url: '<?php echo base_url('api/editCommodity');?>',
                    type: 'post',
                    data: $(form).serialize(),
                    beforeSend: function () {
                        showProgressNoty();
                    },
                    complete: function () {
                        closeProgressNoty();
                    },
                    success: function (jsonrsp) {
                        try {
                            var rsp = $.parseJSON(jsonrsp);
                            noty({type: rsp.type, text: rsp.message, callback: {
                                afterClose: function () {
                                    switch (rsp.type) {
                                        case 'success':
                                            location.reload();
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }});
                        } catch (ex) {
                            noty({type: 'error', text: '发生内部错误，请联系管理员'});
                            throw ex;
                        }
                    }
                });
            }
        });

        UE.getEditor('commodity-content');
       $("#release-type").change(function () {
            if ($("#release-type").val() == "3") {
                $('#commodity-price').val("0");
                $('#commodity-price').attr("readonly", "readonly");
            } else {
                $('#commodity-price').removeAttr("readonly");
                $('#commodity-price').val("");
            }
        });
        if ($("#release-type").val() == "3") {
            $('#commodity-price').val("0");
            $('#commodity-price').attr("readonly", "readonly");
        }


            var $commodityCategory = $('#commodity-category');
            var $subCatSelect      = $('#commodity-sub-category');
            $commodityCategory.val($commodityCategory.find("option[selected]").val());
            if($subCatSelect.find("option[selected]").val())
            {
                $subCatSelect.val($subCatSelect.find("option[selected]").val());
            }else{
                showSubCatByFatherId($commodityCategory.find("option[selected]").val());
            }

            function showSubCatByFatherId(fatherId) {
                var $nowToShowSubCat = $('option[data-father-id=' + fatherId + ']');
                $subCatSelect.children().hide();
                $nowToShowSubCat.show();
                if($nowToShowSubCat.val()){
                    $subCatSelect.val($nowToShowSubCat.first().val());
                }else{
                    $subCatSelect.hide();
                    $commodityCategory.css('width', '100%');
                }

            }

            $commodityCategory.click(function () {
                var nowMasterCatId = $(this).val();
                $subCatSelect.children().hide();
                var $nowToShowSubCat = $('option[data-father-id=' + nowMasterCatId + ']');
                if ($nowToShowSubCat.length) {
                    $nowToShowSubCat.show();
                    $subCatSelect.show();
                    $commodityCategory.css('width', '32%');
                } else {
                    $subCatSelect.hide();
                    $commodityCategory.css('width', '100%');
                }
                $subCatSelect.val($nowToShowSubCat.first().val());
            });
    });
</script>